/************************/
/*	process.c			*/
/************************/
#include "../systeminc/version.h"
#include "../systeminc/system.h"
#include "../ohta/ohta.h"
//#include "../dwaf/dwafTestProc.h"
#include "../systeminc/map.h"
//#include "../systeminc/ime_sa.h"
//#include "../caryIme.h"
#include "../systeminc/menu.h"
#include "../systeminc/pc.h"
#include "../systeminc/character.h"
#include "../systeminc/login.h"
#include "../systeminc/netproc.h"
#include "../systeminc/savedata.h"
#include "../systeminc/testView.h"
#include "../systeminc/battleProc.h"
#include "../systeminc/produce.h"
#include "../systeminc/lssproto_cli.h"
#include "../systeminc/netmain.h"
#include "../systeminc/battleMenu.h"
#include "../systeminc/t_music.h"
#include "../systeminc/field.h"
#include "../systeminc/handletime.h"


/* ???????? *******************************************************************/
extern void kakushi_command(void);
/* ????�k? */
unsigned int ProcNo;
/* ??????�k? */
unsigned int SubProcNo;
/* ????�k?(????????��?�v??) */
int ProcNo2;
int SubProcNo2;

int palNo;
int oldPalNo;
int palTime;
extern SDL_Surface* screenSurface;
extern SDL_Surface* battleSurface;

#ifdef _PET_TRACE_MOUSE
extern SCPlayPet PlayPet;
#endif

/* ?????? ********************************************************************/
void Process( void )
{
	if (ProcNo2 >= 0)
	{
		ProcNo = ProcNo2;
		ProcNo2 = -1;
		SubProcNo = SubProcNo2;
	}
	/* ????��? */
	switch (ProcNo)
	{
		case PROC_OPENNING:
			break;
		case PROC_INIT: 	/* ??????? */
			InitProc();
			initMapEffect(true);		// ???????????
			break;
		// ????????�V???
		case PROC_ID_PASSWORD:			
			BackBufferDrawType = DRAW_BACK_NORMAL; 
			idPasswordProc();
#ifdef _PET_TRACE_MOUSE
			PlayPet.Proc();
#endif
			RunAction();			// ?????????
			StockTaskDispBuffer();	// ???����????????????
			FlashKeyboardCursor();	// ?????????����??
			//ImeProc();				// ???????
#ifdef _LOGINKICK			
			extern unsigned long StartTime;
			StartTime = -1 ; 
#endif

			break;
		// ????����?????�k��
		case PROC_TITLE_MENU:
			// ????????��?������?
			BackBufferDrawType = DRAW_BACK_NORMAL; 
			titleProc();
			TitleProduce();			// ?????��
			RunAction();			// ?????????
			StockTaskDispBuffer();	// ???����????????????
//cary 2002.1.4			kakushi_command();		// ????????
			break;
		// ???�k��?��
		case PROC_CHAR_SELECT:		// ???�k��
			// ????????��?������?
			BackBufferDrawType = DRAW_BACK_NORMAL; 
			selectCharacterProc();
			break;
		// ????��
		case PROC_CHAR_MAKE:
			// ????????��?������?
			BackBufferDrawType = DRAW_BACK_NORMAL; 
			makeCharacterProc();
			break;
		// �k��??????????��
		case PROC_CHAR_LOGIN_START:
			// ????????��?������?
			BackBufferDrawType = DRAW_BACK_NORMAL; 
			// charLoginStart();???????????????????
			// ?????????????
			initMap();		// ??????
			initPcAll();	// PC�ѩ�?????????
			initCharObj();	// ????????????
			initOpenServerWindow();	// ????����?����???????????
			initFieldProc();		// ??????������????
			initMapEffect(false);		// ???????????
			EncountFlag = false;
			logOutFlag = false;
			InitOhtaParam();	// ??????????�^���M??
#ifdef __SKYISLAND
			extern void SkyIslandInit();
			SkyIslandInit();
#endif
			ChangeProc( PROC_CHAR_LOGIN );
			// ???????????��
			fade_out_bgm();
			break;
		case PROC_CHAR_LOGIN:
			// ????????��?������?
			BackBufferDrawType = DRAW_BACK_NORMAL; 
			characterLoginProc();
			break;
		// ?????
		case PROC_CHAR_LOGOUT:
			// ????????��?������?
//			BackBufferDrawType = DRAW_BACK_NORMAL; 
			characterLogoutProc();
			break;
		case PROC_GAME:     /* ??????? */
			GameProc();
			break;
		case PROC_DISCONNECT_SERVER:
			switch (SubProcNo)
			{
			case 0:
				// ???
				// PC????
				resetPc();
				// ?????????????
				initCharObj();
				// ??????��
				DeathAllAction();
				// ?��????
				ProduceInitFlag = true;
				// ????????????�� 
				CopyBackBuffer();
				// ????????��?������?
				BackBufferDrawType = DRAW_BACK_PRODUCE; 
				// ???????????��
				fade_out_bgm();
				// ????????
				NowTime = TimeGetTime();
				SubProcNo++;
				break;
			case 1:
				// ?��?
				if (DrawProduce(PRODUCE_DOWN_ACCELE) == true)
				{
					BackBufferDrawType = DRAW_BACK_NORMAL; 
					SubProcNo++;
				}
				break;
			case 2:
				// ?��?��??
				break;
			}
			// ���e?????????
			if (disconnectServer())
			{
				// ?????????
				cleanupNetwork();
				// ????????????
				PaletteChange(DEF_PAL, 0);
				// ?????��?
				//cary
				ChangeProc( PROC_ID_PASSWORD );
				SubProcNo = 0;
				// ??????��
				DeathAllAction();
				disconnectServerFlag = false;
				oldDisconnectServerFlag = false;
				break;
			}
			RunAction();			// ?????????
			StockTaskDispBuffer();	// ???����????????????
			break;
		case PROC_BATTLE:     /* ??????? */
			BattleProc();
			break;
#ifdef _DEBUG		
		//case PROC_OHTA_TEST:     /* �^��??????? */
		//	OhtaTestProc();
		//	break;
		//case PROC_TAKE_TEST:     /* �{��????????? */
		//	TakeTestProc();
		//	break;
		//case PROC_DWAF_TEST:	// DWAF???
		//	dwafTestProc();
		//	break;
		case PROC_SPR_VIEW:		// ??????�_????
			SprViewProc();
			break;
		case PROC_ANIM_VIEW:	// ????????�_????
			AnimViewProc();
			break;
		case PROC_SE_TEST:	// ???�_????
			SeTestProc();
			break;
#endif
#ifdef _80_LOGIN_PLAY
		case PROC_80_LOGIN:
			BackBufferDrawType = DRAW_BACK_NORMAL; 
			_80LoginProc();
			RunAction();
			StockTaskDispBuffer();
			break;
#endif
#ifdef _PK2007
		case PROC_PKSERVER_SELECT:
			BackBufferDrawType = DRAW_BACK_NORMAL; 
			selectpkProc();
			break;
#endif
		case PROC_ENDING:
			break;
	}
}

/* ????��??? ***********************************************************/
void ChangeProc( int procNo )
{
	// ????�k????
	ProcNo = procNo;
	// ??????�k?????
	SubProcNo = 0;
}
/* ????��??? ***********************************************************/
void ChangeProc( int procNo, int subProcNo )
{
	// ????�k????
	ProcNo = procNo;
	// ??????�k?????
	SubProcNo = subProcNo;
}
/* ????��??????????????��?�v???**************************/
void ChangeProc2( int procNo )
{
	// ????�k????
	ProcNo2 = procNo;
	SubProcNo2 = 0;
}
/* ????��??? ***********************************************************/
void ChangeProc2( int procNo, int subProcNo )
{
	// ????�k????
	ProcNo2 = procNo;
	// ??????�k?????
	SubProcNo2 = subProcNo;
}

void GameProc( void )
{
	static int now_bgm = map_bgm_no;
	static bool produceFlag;
	switch( SubProcNo ){
		case 0:
			//cary ��ֹ�ٴε���ʱ���Ӵ�����
			extern short helpBtn, actBtn;
			helpBtn = 0;
			actBtn = 0;
			//end cary
#ifdef _DEBUG
			if( offlineFlag ){
				initMap();
			}
#endif
			//InitIme();
			//ClearIme();
			InitChat();
			produceFlag = false;
			EncountFlag = false;
			LoadAlbum();
			extern void AI_Init();
			AI_Init();
			nameOverTheHeadFlag = 0;
			SubProcNo++;
		case 150:
			SubProcNo = 100;

		case 100:
			initPc();
			resetFieldProc();
			restorePtActCharObjAll();
			initOpenServerWindow();
			InitMenu();
//			BattleStatusBak[ 0 ] = NULL;// ???�֢t???
//			BattleStatusReadPointer = BattleStatusWritePointer =0;
			GetKeyInputFocus( &MyChatBuffer );
			SubProcNo++;
		case 101:
			if( loginFlag ){
				BackBufferDrawType = DRAW_BACK_NORMAL; 
				break;
			}
			SubProcNo++;
		case 102:
			paletteProc();

			DispBuffer.DispCnt = 0;
			FontCnt = 0;
			initCharPartsPrio();	// ?????????�I???�e??????
			RunAction();			// ?????????
			StockTaskDispBuffer();	// ???����????????????
			stop_bgm();				//???�_��
			drawMap();				//	???����
			MenuProc();				// ??????
			ChatProc();				// ??????
			ChatBufferToFontBuffer(); // ??????????????????????
			//ImeProc();				// ???????
			SortDispBuffer(); 	// ����???????
			// ???????????????
			ClearBackSurface();	
			// ????????????????
			PutBmp();	
			// ??????????????????????
            SDL_BlitSurface(screenSurface, 0, battleSurface, 0);
			//lpBattleSurface->BltFast( 0, 0, lpDraw->lpBACKBUFFER, NULL, DDBLTFAST_WAIT );
			// ???????
			DispBuffer.DispCnt = 0;
			FontCnt = 0;

			play_bgm( map_bgm_no );

			// ????????
			NowTime = TimeGetTime();
			// ????????��?������?
//			BackBufferDrawType = DRAW_BACK_NORMAL; 
			BackBufferDrawType = DRAW_BACK_PRODUCE; 
			// ?��?
			ProduceInitFlag = true;
			DrawProduce( PRODUCE_BRAN_BIG );
/*#ifdef __SKYISLAND
			ClearBackSurface();
#endif*/
			SubProcNo++;
			break;

		case 103:
			// ?��?
			BackBufferDrawType = DRAW_BACK_PRODUCE; 
			if( DrawProduce( PRODUCE_BRAN_BIG ) == true )
			{
				produceFlag = true;
				fieldInfoTime = TimeGetTime();
				if( fastDrawTile )
				{
					SubProcNo = 20;
				}
				else
				{
					SubProcNo = 3;
				}
			}
			break;

		// ????��
		case 200:
			ProduceInitFlag = true;

			// ???????
			DispBuffer.DispCnt = 0;
			FontCnt = 0;

			// ????????
			NowTime = TimeGetTime();

			// S??????C? warpEffectProc();?????
			// ?��???????????????
			SubProcNo++;

		case 201:
			// ?��?
			BackBufferDrawType = DRAW_BACK_PRODUCE; 
			if( DrawProduce( PRODUCE_CENTER_PRESSIN ) == true )
			{
				SubProcNo++;
			}
			drawField();	// ??????��?????����
			MenuProc();	// ??????
			//ImeProc();	// ???????
			break;

		case 202:
			if( !warpEffectStart || !warpEffectOk )
			{
				BackBufferDrawType = DRAW_BACK_NORMAL; 
				drawField();	// ??????��?????����
				MenuProc();	// ??????
				//ImeProc();	// ???????
				break;
			}
			warpEffectStart = false;
			warpEffectOk = false;
			SubProcNo++;
		case 203:
			// ?��????��
			// ???????
			paletteProc();

			DispBuffer.DispCnt = 0;
			FontCnt = 0;
			initCharPartsPrio();	// ?????????�I???�e??????
			RunAction();			// ?????????
			StockTaskDispBuffer();	// ???����????????????
			redrawMap();
			drawMap();				//	???����

			// ?????????
			if( (mapEffectRainLevel == 0 && oldMapEffectRainLevel != 0 ) 
				|| (mapEffectSnowLevel == 0 && oldMapEffectSnowLevel != 0 )
				)
				initMapEffect(false);
			ChatProc();				// ??????
			ChatBufferToFontBuffer(); // ??????????????????????
			SortDispBuffer(); 	// ����???????
			// ???????????????
			ClearBackSurface();	
			if( (mapEffectRainLevel != 0 && oldMapEffectRainLevel == 0 )
			 || (mapEffectSnowLevel != 0 && oldMapEffectSnowLevel == 0 ) )
				mapEffectProc2( 80 );		// ???????????�D??
			// ????????????????
			PutBmp();	
			// ??????????????????????
			//lpBattleSurface->BltFast( 0, 0, lpDraw->lpBACKBUFFER, NULL, DDBLTFAST_WAIT );
            SDL_BlitSurface(screenSurface, 0, battleSurface, 0);
			// ???????
			DispBuffer.DispCnt = 0;
            FontCnt = 0;

			if( map_bgm_no != now_bgm)
			{
				stop_bgm();				//???�_��
				play_bgm( map_bgm_no );
                now_bgm = map_bgm_no;
			}

			// ????????
			NowTime = TimeGetTime();

			ProduceInitFlag = true;

			SubProcNo++;

		case 204:
			// ?��?
			BackBufferDrawType = DRAW_BACK_PRODUCE; 

			if( DrawProduce( PRODUCE_CENTER_PRESSOUT ) == true )
			{
				fieldInfoTime = TimeGetTime();
				if( fastDrawTile )
				{
					SubProcNo = 20;
				}
				else
				{
					SubProcNo = 3;
				}
	
			}
			drawField();	// ??????��?????����
			MenuProc();	// ??????
			//ImeProc();	// ???????
			break;


		case 1:
			initPc();				// ?????????��
			resetFieldProc();		// ????????????
			restorePtActCharObjAll();	// ?????????????????
			initOpenServerWindow();	// ????����?����???????????
			InitMenu();		// ??????????
//			BattleStatusBak[ 0 ] = NULL;// ???�֢t???
//			BattleStatusReadPointer = BattleStatusWritePointer =0;
			// �V??????��x
			GetKeyInputFocus( &MyChatBuffer );

			// ?��???
			if( produceFlag == true ){
			// �P�f????????????
			//if( BattleResultWndFlag == true ){

				// ?��????��
				// ???????
				DispBuffer.DispCnt = 0;
				FontCnt = 0;
				initCharPartsPrio();	// ?????????�I???�e??????
				RunAction();			// ?????????
				StockTaskDispBuffer();	// ???����????????????
				stop_bgm();				//???�_��
				updateMapArea();
				redrawMap();
				drawMap();				//	???����
				//????????
				if(draw_map_bgm_flg){
					//????????��
					play_bgm(map_bgm_no = now_bgm);
					draw_map_bgm_flg = 1;
				}
				MenuProc();				// ??????
				// ???????�D??????��?
				//StockBoxDispBuffer( 0, 456, 640, 480, DISP_PRIO_MENU, 0, 1 );
				ChatProc();				// ??????
				ChatBufferToFontBuffer(); // ??????????????????????
				//ImeProc();				// ???????
				SortDispBuffer(); 	// ����???????
				// ???????????????
				ClearBackSurface();	
				// ????????????????
				PutBmp();	
				// ??????????????????????
				//lpBattleSurface->BltFast( 0, 0, lpDraw->lpBACKBUFFER, NULL, DDBLTFAST_WAIT );
                SDL_BlitSurface(screenSurface, 0, battleSurface, 0);
				// ???????
				DispBuffer.DispCnt = 0;
				FontCnt = 0;
				// ????????
				NowTime = TimeGetTime();
				// ????????��?������?
				BackBufferDrawType = DRAW_BACK_PRODUCE; 
				// ?��?
				DrawProduce( PRODUCE_4WAY_IN );
			}else{
				// ?��?????
				produceFlag = true;
				SubProcNo++;
			}
			SubProcNo++;
			
			break;
		
		case 2:	// ????????��
			// ?��?
			if( DrawProduce( PRODUCE_4WAY_IN ) == true ){
				warpEffectStart = false;
				warpEffectOk = false;
				if( fastDrawTile )
				{
					SubProcNo = 20;
				}
				else
				{
					SubProcNo = 3;
				}
			}
			break;


		case 20:
			BackBufferDrawType = DRAW_BACK_NORMAL; 
			repairMap();
			SubProcNo = 3;

		case 3:
			// ????????��?������?
			
			BackBufferDrawType = DRAW_BACK_NORMAL; 
			paletteProc();

			initItemOverlapCheck();	// ????��????????????

			if( !transmigrationEffectFlag ){
				drawGrid();				// ????????����
				fieldProc();			// ?????????
				// Nuke 0407
			if( bNewServer){
				//if (SendCount>0)		//Jerry 速度限制 服务端做了时间限制
					moveProc();				// ?�h??
			}else
				moveProc();				// ?�h??
			}

			initCharPartsPrio();	// ?????????�I???�e??????
			//addressBookProc();		// ?????????
			openServerWindowProc();	// ????��?????
			drawFieldInfoWin();		// ?????��?�ѩ�
			
			/* ????????? */
			RunAction();
			// ???����????????????
			// ���e�d?????????
			StockTaskDispBuffer();

			mapEffectProc();		// ???????????�D??

			drawMap2();		// ???����

            if( !transmigrationEffectFlag ){
			    drawField();	// ??????��?????����

			/* ?????? */
			    ChatProc();
			// ??????????????????????
			    ChatBufferToFontBuffer(); 
			// ?????????����??
			    FlashKeyboardCursor();
			// ??????
			    MenuProc();
			// ???????
			    //ImeProc();		
			}
			// ??�q?????????????
			TimeZoneProc();
			
/*cary
#ifdef _DEBUG
			// ?????????
			if( joy_trg[ 0 ] & JOY_P_DOWN ){
				if( pc.ptAct != NULL ){
					resetMap();					// ?????????
					// ???????��
					if( bNewServer)
						lssproto_EN_send( sockfd, pc.ptAct->gx, pc.ptAct->gy );
					else
						old_lssproto_EN_send( sockfd, pc.ptAct->gx, pc.ptAct->gy );
					eventEnemyFlag = 0;
				}
			}
#endif
*/
			// ?????????
			if( EncountFlag == true ){
				resetPc();				// PC????
				resetCharObj();			// ?????????????
				resetMap();				// ?????????
				clearPtActPartyParam();	// ??�ѩ�?????????????NULL???
				fieldInfoTime = 0;		// ��?�ѩ�?��???��?
				drawFieldInfoWin();		// ?????��?�ѩ�
				resetFieldProc();		// ????????????
				nowEncountPercentage = minEncountPercentage;// ?????????��???
				sendEnFlag = 0;
				encountNowFlag = 1;
				eventEnemySendFlag = 0;
				duelSendFlag = 0;
				jbSendFlag = 0;
				// ?????????????????
				if( MenuToggleFlag & JOY_CTRL_M ) MapWmdFlagBak = true;
				// ��???????????
				ResultWndTimer = RESULT_WND_CLOSE_TIME;
				InitMenu();			// ??????????
				BattleCmd[ 0 ] = NULL;		// ??????????
//				BattleCmdBak[ 0 ] = NULL;	// ??????????
//				BattleCmdReadPointer = BattleCmdWritePointer =0;
				BattleStatus[ 0 ] = NULL;	// ???�֢t???
				//BattleStatusBak[ 0 ] = NULL;// ???�֢t???
//				BattleStatusReadPointer = BattleStatusWritePointer =0;
				BattleTurnReceiveFlag = true;	// ???����?????
				// ???????
				play_se( 215, 320, 240 );
				// ??????��V
				now_bgm = t_music_bgm_no;
				// ???�_��
				stop_bgm();
				//SubProcNo++;
				ChangeProc2( PROC_GAME, SubProcNo+1 );
			}
			break;
			
		case 4:	// ??????????��??????????
			// ????????????�� 
			CopyBackBuffer();
			// ????????��?������?
			BackBufferDrawType = DRAW_BACK_PRODUCE; 
			SubProcNo++;
			
			break;
			
		case 5:	// ???????��
		
			// ?��?
			if( DrawProduce( PRODUCE_HAGARE_OCHI_OUT ) == true ){
			//if( DrawProduce( PRODUCE_RIGHT_ACCELE ) == true ){
			//if( DrawProduce( PRODUCE_LEFT_RIGHT_ACCELE ) == true ){
			//if( GameState == GAME_ENCOUNT_TO_BATTLE ){ 
				// ????????
#ifdef _HALLOWEEN_EFFECT
				initMapEffect(false);	
#endif
				ChangeProc( PROC_BATTLE );
			}
			MenuProc();	// ??????
			//ImeProc();	// ???????
			
			break;
	}
}


// ????��?
void warpEffectProc( void )
{
	oldMapEffectRainLevel = mapEffectRainLevel;
	oldMapEffectSnowLevel = mapEffectSnowLevel;

	DispBuffer.DispCnt = 0;
	FontCnt = 0;

	// ?????????????????
	if( MenuToggleFlag & JOY_CTRL_M ) MapWmdFlagBak = true;
	InitMenu2();

	fieldProc();			// ?????????
	initCharPartsPrio();	// ?????????�I???�e??????
	openServerWindowProc();	// ????��?????

	fieldInfoTime = 0;		// ��?�ѩ�?��???��?
	drawFieldInfoWin();		// ?????��?�ѩ�


	/* ????????? */
	RunAction();
	// ???����????????????
	StockTaskDispBuffer();

	mapEffectProc();		// ???????????�D??

	redrawMap();
	drawMap();		// ???����
	/* ?????? */
	ChatProc();
	// ??????????????????????
	ChatBufferToFontBuffer(); 
	// ??�q?????????????
//	TimeZoneProc();
	SortDispBuffer(); 	// ����???????
	// ???????????????
	ClearBackSurface();	
	// ????????????????
	PutBmp();	

#ifndef __SKYISLAND
	// ??????????????????????
	lpBattleSurface->BltFast( 0, 0, lpDraw->lpBACKBUFFER, NULL, DDBLTFAST_WAIT );
#endif
}


void repairMap( void )
{
	float dx, dy;

	//
		camMapToGamen( 0.0, 0.0, &dx, &dy );

	nowXFastDraw = (int)(dx+.5);
	nowYFastDraw = (int)(dy+.5);

	nowXFastDraw2 = nowXFastDraw;
	nowYFastDraw2 = nowYFastDraw;

	//???????????????
	if( ResoMode == 1 ){
		nowXFastDraw = (int)(dx/2+.5);
		nowYFastDraw = (int)(dy/2+.5);
	}

	baseXFastDraw = nowXFastDraw;
	baseYFastDraw = nowYFastDraw;
	amountXFastDraw = nowXFastDraw - baseXFastDraw;
	amountYFastDraw = nowYFastDraw - baseYFastDraw;

	DispBuffer.DispCnt = 0;
	FontCnt = 0;
	drawTile();		// ???����
	SortDispBuffer(); 	// ����???????
	// ???????????????
	//cary 2001 11 8
	ClearBackSurface();	

	// ????????????????
	fastDrawTileFlag = 0;
	PutBmp();	
	fastDrawTileFlag = 1;
#ifndef __SKYISLAND
	// ??????????????????????
	lpBattleSurface->BltFast( 0, 0, lpDraw->lpBACKBUFFER, NULL, DDBLTFAST_WAIT );
#endif
	DispBuffer.DispCnt = 0;
	FontCnt = 0;
	// ????????
	NowTime = TimeGetTime();
}


void paletteProc( void )
{
	if( palNo == -1 )
	{
		// ????????
		// the second PaletteChange( SaTimeZoneNo, 0 );
		PaletteChange( SaTimeZoneNo, palTime );
		// ??�q????????????
		TimeZonePalChangeFlag = true;
		palNo = -2;
	}
	else
	if( palNo >= 0 )
	{
		// ?�e????�@�e
		// the second PaletteChange( palNo, 0 );// ????????
		PaletteChange( palNo, palTime );// ????????
		// ??�q?????????????
		TimeZonePalChangeFlag = false;
		palNo = -2;
	}
}

#ifdef _SURFACE_ANIM
extern ACTION *SPACT[MAX_ANIM];
void AniProc()
{
	float mx,my;

	for (int i = 0; i < MAX_ANIM; i++)
	{
		if(SPACT[i] != NULL)
		{
			camMapToGamen(SPACT[i]->mx, SPACT[i]->my, &mx, &my);
			SPACT[i]->x = (int)(mx + .5);
			SPACT[i]->y = (int)(my + .5);
			pattern(SPACT[i], ANM_NOMAL_SPD, ANM_LOOP);
		}
	}
}
#endif